package sz.szsmk.citizencard.ui.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.util.List;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.model.network.bean.response.HomeHeadFuncResponse;
import sz.szsmk.citizencard.ui.newversion.config.Constants;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.JumpUtils;

/**
 * Copyright (c)2019
 * author: zdh
 * created on: 2019/1/3 14:34
 * description:
 */
class ApphomeHeadMyRvGridAdapter extends RecyclerView.Adapter<ApphomeHeadMyRvGridAdapter.ApphomeMyRvHolder> {
    private Context mContext;
    private List<HomeHeadFuncResponse.FunctionListBean> viewData;
    private String type;//type "head" 头部适配器  "home" 主体适配器

    public ApphomeHeadMyRvGridAdapter(Context mContext, String type) {
        this.mContext = mContext;
        this.type = type;
    }


    private BaseInterface baseInterface;

    public interface BaseInterface {
        public void onClick(View view, String url, String syslabl, int need_header, String s, HomeHeadFuncResponse.FunctionListBean bean);
    }

    public void setBaseInterface(BaseInterface baseInterface) {
        this.baseInterface = baseInterface;
    }

    @Override
    public ApphomeMyRvHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.home_head_grid_view, parent, false);
        return new ApphomeMyRvHolder(view);
    }

    @Override
    public void onBindViewHolder(ApphomeMyRvHolder holder, final int position) {
        if (viewData != null) {

            holder.itemTv.setText(viewData.get(position).getFunction_name());
            if (viewData.get(position).getIcon_url() != null && viewData.get(position).getIcon_url().equals("1")) {
                holder.itemIv.setImageResource(R.drawable.apphome_more);
            } else {

                if ("home".equals(type)) {
                    Glide.with(mContext)
                            .load(viewData.get(position).getIcon_url())
                            .into(holder.itemIv);
                }
                if ("head".equals(type)) {
                    switch (position) {
                        case 0:
                            Glide.with(mContext).load(R.mipmap.icon_head_trip).into(holder.itemIv);
                            break;
                        case 1:
                            Glide.with(mContext).load(R.mipmap.icon_head_scan).into(holder.itemIv);
                            break;
                        case 2:
                            Glide.with(mContext).load(R.mipmap.icon_head_recharge).into(holder.itemIv);
                            break;
                        case 3:
                            Glide.with(mContext).load(R.mipmap.icon_head_wallet).into(holder.itemIv);
                            break;
                    }
                }


            }
            holder.viewLL.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {


                    switch (position) {
                        case 0:
                            JumpUtils.jump(mContext, "tripPlan", 1);
                            break;
                        case 1:
                            JumpUtils.jump(mContext,  Constants.SCANQRCODECHECKACTIVITY, 1);
                            break;
                        case 2:
                            JumpUtils.jump(mContext, "rechargeActivity", 1);
                            break;
                        case 3:
                            JumpUtils.jump(mContext, "cardListActivity", 1);
                            break;
                        default:
                            break;
                    }

                    // HomeHeadFuncResponse.FunctionListBean bean = viewData.get(position);
                    //  String url = bean.getLink_url();

                    //  baseInterface.onClick(v, url, bean.getSys_label(), bean.getNeed_header(),bean.getFunction_id()+"",bean);


                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return viewData.size();
    }

    public void setViewData(List<HomeHeadFuncResponse.FunctionListBean> viewData) {

        this.viewData = viewData;
    }


    public static class ApphomeMyRvHolder extends RecyclerView.ViewHolder {

        private final ImageView itemIv;
        private final TextView itemTv;
        private final LinearLayout viewLL;

        public ApphomeMyRvHolder(View itemView) {
            super(itemView);
            itemIv = (ImageView) itemView.findViewById(R.id.user_grid_icon);
            itemTv = (TextView) itemView.findViewById(R.id.user_grid_tv);
            viewLL = (LinearLayout) itemView.findViewById(R.id.user_grid_ll);
        }
    }

}
